<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/
/**
 * Update a topic
*/

/**
 * Update a user
 * @author John Cox
 * @author Jo dalle Nogare
 * @param $args['tagcount'] integer
 * @param $args['timestamp'] integer
 * @param $args['postcount'] integer
 * @param $args['uid'] integer
 * @param $args['options'] string
 * @returns int
 * @return true on success, NULL on failure
 * @todo support an 'options' array that merges into an existing 'toptions' array.
 */

function conversations_userapi_updateuser($args)
{
    // Get arguments from argument array
    extract($args);

    if (!isset($uid)) $invalid[] = "uid";

    // params in arg
    $params = array(
        "tagcount"  => "xar_tagcount",
        "timestamp" => "xar_timestamp",
        "postcount" => "xar_postcount",
        "uid"       => "xar_uid",
        "options"   => "xar_options"
    );

    foreach($params as $vvar => $dummy) {
        if (isset($args[$vvar])) {
            $set = true;
            break;
        }
    }

    if (!isset($set)) {
        $invalid[] = xarML('At least one parameter has to be set: #(1)', join(',', array_keys($fields)));
    }
    // Argument check - make sure that at least on paramter is present
    // if not then set an appropriate error message and return
    if (isset($invalid)) {
        $msg = xarML('Invalid parameter count');
        xarErrorSet(XAR_SYSTEM_EXCEPTION, 'BAD_PARAM', new SystemException($msg));
        return;
    }

    // Get datbase setup
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['cusers'];

    if ((empty($timestamp)) && (empty($options))) {
        $timestamp = time();
    }

    $update = array();
    $bindvars = array();

    // Update item
    $query = "UPDATE $table SET ";
    if (isset($uid)){
        $update[] = "xar_uid = ? ";
        $bindvars[] = $uid;
    }
    if (isset($tagcount)){
        $update[] = "xar_tagcount = ? ";
        $bindvars[] = $tagcount;
    }
    if (isset($timestamp)){
        $update[] = "xar_timestamp = ? ";
        $bindvars[] = $timestamp;
    }
    if (isset($postcount)){
        $update[] = "xar_postcount = ? ";
        $bindvars[] = $postcount;
    }
    if (isset($options)){
        $update[] = "xar_options = ? ";
        $bindvars[] = $options;
    }
    $query .= join(",",$update);
    $query .= "WHERE xar_uid = ? ";
    $bindvars[] = $uid;
    $result =& $dbconn->Execute($query, $bindvars);
    if (!$result) return;
    
    return true;
}
?>